import 'dart:convert';

import 'package:flutter/material.dart';
import '../detail/detail.dart';

// 引入dio 库 进行数据请求
import 'package:dio/dio.dart';

// 动态组件
class MovieList extends StatefulWidget {
  @override
  _MovieListState createState() => _MovieListState();
}

// 控制器： 动态组件的状态 在这边管理
class _MovieListState extends State<MovieList> {
  // final List list = new CustomData().list;
  List list = [];

  @override
  void initState() {
    getMovieList();
    super.initState();
  }

  // 请求数据
  getMovieList() async {
    try {
      Dio dio = Dio();
      Response response = await dio.get(
          'http://m.maoyan.com/ajax/movieOnInfoList?token=');
      Map responseData = jsonDecode(response.toString());
      // 赋值状态
      setState(() {
        list = responseData["movieList"];
      });
    } catch (e) {
      print(e);
    }
  }

  @override
  Widget build(BuildContext context) {
    String _imgReset(String img) => img.replaceAll('/w.h/', '/128.180/');
    return ListView.builder(
      itemCount: list.length,
      itemBuilder: (BuildContext context, int i) {
        Map _myitem = list[i];
        // 添加事件 要用这个组件包裹
        return GestureDetector(
          onTap: (){
            Navigator.of(context).push(MaterialPageRoute(
              builder: (BuildContext context){
                return Detail(id: _myitem["id"], nm: _myitem["nm"]);
              }
            ));
          },
          child: Container(
            padding: EdgeInsets.all(10),
            child: Row(
              children: <Widget>[
                Image(
                  width: 128,
                  height: 179,
                  image: NetworkImage('${_imgReset(_myitem["img"])}'),
                ),
                Expanded(
                  child: Container(
                    padding: EdgeInsets.only(left: 10),
                    height: 160,
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.spaceAround,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        Text(
                          '${_myitem["nm"]}',
                          style: TextStyle(
                            fontSize: 17,
                          ),
                          overflow: TextOverflow.ellipsis,
                          maxLines: 1,
                        ),
                        Text('评分：${_myitem["sc"]}'),
                        Text(
                          '主演：${_myitem["star"] ?? "未知"}',
                          overflow: TextOverflow.ellipsis,
                          maxLines: 1,
                        ),
                        Text(
                          '上映日期：${_myitem["rt"] ?? "未知"}',
                          overflow: TextOverflow.ellipsis,
                          maxLines: 1,
                        ),
                        Text(
                          '排期：${_myitem["showInfo"] ?? "未知"}',
                          overflow: TextOverflow.ellipsis,
                          maxLines: 1,
                        ),
                      ],
                    ),
                  ),
                ),
              ],
            ),
          ),
        );
      },
    );
  }
}
